package cn.jji8.patronSaint.quick.command;

import org.bukkit.command.CommandSender;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@SuppressWarnings("unused")
class CommandData {
    /**
     * 命令的触发器
     * */
    public final String[] command;
    /**
     * 默认的补全参数
     * */
    public final String[][] completion;
    /**
     * 对应的命令执行器
     * */
    public final CommandImplement commandImplement;
    /**
     * 执行此命令需要的权限
     * */
    public final String authority;

    /**
     * 普通的构造器
     * @param command 触发的命令
     * @param completion 补全的命令
     * @param commandImplement 执行器
     * */
    CommandData(String[] command, String[][] completion,String authority, CommandImplement commandImplement) {
        this.command = command;
        this.completion = completion;
        this.commandImplement = commandImplement;
        this.authority = authority;
    }
    /**
     * 玩家执行了命令，
     * */
    public boolean implement(CommandSender commandSender,String[] command){
        if(command.length<1){
            return false;
        }
        if(!ifImplement(command[0])){
            return false;
        }
        //命令参数
        String[] strings =new String[command.length-1];
        System.arraycopy(command, 1, strings, 0, command.length - 1);
        //调用方法
        commandImplement.command(commandSender,strings);
        return true;
    }
    /**
     * Tab补全参数，
     * */
    public List<String> TabComplete(CommandSender commandSender, String[] strings) {
        //优先调用，命令执行器的补全器
        List<String> list = commandImplement.TabCompletion(commandSender,strings);
        if(list!=null){
            return list;
        }
        //默认的补全器
        list = new ArrayList<>();
        if(completion.length>=strings.length-1){
            Collections.addAll(list, completion[strings.length - 2]);
            return list;
        }
        return null;
    }
    /**
     * 判断这个参数是会触发我
     * */
    public boolean ifImplement(String s){
        for(String i:command){
            if(i.equalsIgnoreCase(s)){
                return true;
            }
        }
        return false;
    }

}
